package com.yootk.boot.action;

import com.yootk.dto.DeptDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController // 按照正规的设计肯定要在前端准备出thymeleaf页面进行内容展示
public class DeptConsumerAction {
    public static final String DEPT_ADD_URL = "http://localhost:8001/dept/add" ;
    public static final String DEPT_GET_URL = "http://localhost:8001/dept/get" ;
    public static final String DEPT_LIST_URL = "http://localhost:8001/dept/list" ;

    /**
     * rest接口模板,（url,requestMap,ResponseBean.class）:rest请求地址、请求参数，Http相应被转换成的对象类型
     */
    @Autowired
    private RestTemplate restTemplate ;

    @GetMapping("/boot/dept/add")
    public Object add(DeptDTO dto) {
        return this.restTemplate.postForObject(DEPT_ADD_URL,dto,DeptDTO.class) ;
    }

    @GetMapping("/boot/dept/get/{id}")
    public Object get(@PathVariable("id") long id) {
        return this.restTemplate.getForObject(DEPT_GET_URL + "/" + id,DeptDTO.class) ;
    }

    @GetMapping("/boot/dept/list")
    public Object list() {
        return this.restTemplate.getForObject(DEPT_LIST_URL, List.class);
    }
}
